package aad.storyboard.transitions;

import java.util.ArrayList;
import java.util.HashMap;

import utils.ViewUtils;

import aad.storyboard.controller.GUIComposer;
import aad.storyboard.controller.SceneTransition;
import aad.storyboard.controller.TransitionProvider;
import aad.storyboard.model.Animation;
import aad.storyboard.model.BlackFader;
import aad.storyboard.model.BlueFader;
import aad.storyboard.model.CrossFadeAnimation;
import aad.storyboard.model.Fader;
import aad.storyboard.model.GreenFader;
import aad.storyboard.model.Image;
import aad.storyboard.model.RedFader;
import aad.storyboard.model.YellowFader;
import aad.storyboard.view.GraphicView;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Bitmap.Config;
import android.graphics.ColorMatrix;
import android.graphics.ColorMatrixColorFilter;
import android.graphics.Paint;
import android.os.Handler;
import android.os.Message;

public class ColorFadeTransition extends SceneTransition{
	
	private static Paint mForeGroundPaint;  
    private static Paint mBackGroundPaint; 
   
    private static ArrayList<Bitmap> characterList=new ArrayList<Bitmap>(); 
     
    private static int foregrndalpha =255;  
    private static int mAlpha = 0; 
    private static int duration;
    private static Runnable mAnimationTask;  
    private FadInFadeOutHandler mHandler=new FadInFadeOutHandler();
   
    private HashMap<Integer, Fader> fadeStrategies;
    private Fader fader;

    //private Bitmap contentBitmap;    
    private static Bitmap inextCharacter;  
    private static ArrayList <Image> icurrentCharacters;   
    private ColorMatrix cm;
   
    public ColorFadeTransition(int startTime, int duration, int fadeTagetColor) 
    {  
    	super(startTime,duration);   	
    	mForeGroundPaint = new Paint();  
    	mBackGroundPaint = new Paint(); 
    	
    	cm = new ColorMatrix();   
    	
    	inextCharacter=TransitionProvider.getInstance().getFadeBitmap();
    	
     fadeStrategies=TransitionProvider.getInstance().getFadeStratiegies();
    
     this.fader=fadeStrategies.get(fadeTagetColor);
      
     }  
      
    class FadInFadeOutHandler extends Handler  
    {  
     @Override
	public void handleMessage(Message m)  
     {  
      if(m.what == 0)  
      {  
       System.out.println("Handle message");  
      
      }  
         
     }  
    }

	@Override
	public boolean execute() {	
		  
		setStarted(true);
		System.out.println("drin");
		 mAnimationTask = new Runnable()  
	     {  
	      
	       @Override
		public void run()
	       {  
	    	 
	    	icurrentCharacters=((GraphicView) (GUIComposer.getInstance().getGraphicalView())).getImages();
	    	
	    	if (icurrentCharacters!=null)
	    	{
	    		
	    		for (Image img: icurrentCharacters)
	       	{
	       		img.setPaint(mForeGroundPaint);
	       		
	       	} 
	         
	        if(foregrndalpha > 0 )  
	        {  
	         System.out.println("drin");         
	          foregrndalpha -= 50;  
	          mAlpha+=50;
	          mForeGroundPaint.setAlpha(foregrndalpha); 
	          
	          fader.fade(cm, mAlpha);
	          mBackGroundPaint.setColorFilter(new ColorMatrixColorFilter(cm));       
	         
	         ((GraphicView)(GUIComposer.getInstance().getGraphicalView())).setFadeTarget(inextCharacter, mBackGroundPaint);         
	         
	         mHandler.sendEmptyMessage(0);  
	         mHandler.postDelayed(mAnimationTask, 100);  
	        }
	    	}
	       }  
	     }; 
	     
	    mHandler.postDelayed(mAnimationTask, 100);
	    return true;
	}

	     

}

